const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
    entry: path.resolve(__dirname, "../src/index.js"),
    output: {
        path: path.resolve(__dirname, "../build"),
        filename: "./js/main.js",
        publicPath:"/"  //访问二级路由的时候，会默认把之前的url都认为是路径（把一级路径的路径名字也算进去），查询失败，报错无法找到此路径上的文件，因此需要设置公共路径为根目录，这样，无论是一级二级还是之后的都会从根目录查找路径了
    },
    module: {
        rules: [
            /* {
                test: /\.html$/i,
                use: [
                    'html-loader'
                ]
            }, */
            {
                test: /\.ejs$/i,
                loader: 'ejs-loader',
                // 可以在模块当中使用data接受外部传入ejs的数据进行数据处理
                options: {
                    variable: 'data',
                }
            },
            {
                test: /\.css$/i,
                use: [
                    //use中loader加载是有顺序的，先下后上，注意有的loader需要按照顺序书写
                    "style-loader",
                    'css-loader'
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, "../public/index.html")
        }),
        new CopyWebpackPlugin({
            patterns: [{
                from: path.resolve(__dirname, "../public"),
                to: "public",
                globOptions: {
                    ignore: ["**/index.html"]
                }
            }],
        })
    ],
    resolve: {
        alias: {
            $v: path.resolve(__dirname, "../src/views"),
            $r: path.resolve(__dirname, "../src/routes"),
            $a: path.resolve(__dirname, "../src/api"),
        },

        extensions: [".js", ".json", ".ejs"]
    },

    mode: "production"
}